faceFusionAPI.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. *
  3. * @authors Eric Hsiao
  4. *
  5. */
  6. faceFusionAPI = function () {
  7. //private menbers
  8. // var API__DOMAIN = 'https://ncxpre.cecmartech.com/'
  9. const API__DOMAIN = (window.location.host === 'www.pxfreshdelivery.tw') ? 'https://www.pxfreshdelivery.tw/' : 'https://demo.pxfreshdelivery.tw/';
  10. var templateData;
  11. var photoData;
  12. var faceFusionAlpha = 0.49;
  13. var callback = function () { };
  14. //private methods
  15. function init() {
  16. console.log('faceFusion is loaded.');
  17. }
  18. function start(_templateURL, _photoData, _faceFusionAlpha, _callback) {
  19. photoData = _photoData;
  20. faceFusionAlpha = _faceFusionAlpha;
  21. callback = _callback;
  22. // templateData = getBase64Image(_templateURL);
  23. templateData = _templateURL;
  24. console.log('faceFusionAPI 啟動');
  25. $('.faceFusion__start').hide();
  26. faceFusion();
  27. }
  28. function faceFusion() {
  29. var templateData_BASE64 = templateData.replace(/^data:image\/\w+;base64,/, "");//去掉base64標頭
  30. var photoData_BASE64 = photoData.replace(/^data:image\/\w+;base64,/, "");//去掉base64標頭
  31. console.log('faceFusionAPI 發送中...');
  32. $('.msg').text('faceFusionAPI 發送中...');
  33. $.ajax({
  34. type: "POST",
  35. url: `${API__DOMAIN}web/faceMerge`,
  36. data: {
  37. "version": "4.0",
  38. "alpha": faceFusionAlpha,
  39. "image_template": {
  40. "image": templateData_BASE64,
  41. "image_type": "BASE64"
  42. },
  43. "image_target": {
  44. "image": photoData_BASE64,
  45. "image_type": "BASE64"
  46. }
  47. },
  48. dataType: "json",
  49. success: function (response) {
  50. console.log(response);
  51. if (response.error_msg == 'SUCCESS') {
  52. var _merge_image = 'data:image/png;base64,' + response.result.merge_image;
  53. // $('.faceFusion').attr('src', _merge_image);
  54. callback(_merge_image);
  55. $('.msg').text('合成成功。');
  56. $('.faceFusion__start').show();
  57. } else {
  58. // console.log('faceFusionAPI 失敗重送');
  59. // setTimeout(function () {
  60. // faceFusion();
  61. // }, 1000);
  62. $('.msg').text('圖片下載失敗,請重新送出。');
  63. alert('圖片下載失敗,請重新送出。');
  64. $('.faceFusion__start').show();
  65. }
  66. },
  67. error: function (XMLHttpRequest, textStatus, errorThrown) {
  68. console.log("Status: " + textStatus + ", Error: " + errorThrown);
  69. }
  70. });
  71. }
  72. //constructor
  73. function getBase64Image(imgURL, width, height) { // width、height調用時傳入具體像素值,控制大小 ,不傳則默認圖像大小
  74. var img = new Image();
  75. img.onload = function () {
  76. var canvas = document.createElement("canvas");
  77. canvas.width = width ? width : img.width;
  78. canvas.height = height ? height : img.height;
  79. var ctx = canvas.getContext("2d");
  80. ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
  81. var dataURL = canvas.toDataURL("image/jpeg");
  82. templateData = dataURL;
  83. // console.log('dataURL', dataURL);
  84. console.log('faceFusionAPI 啟動');
  85. faceFusion();
  86. };
  87. img.src = imgURL;
  88. }
  89. {
  90. $(document).ready(function () {
  91. init();
  92. });
  93. }
  94. //public
  95. return {
  96. start: function (_templateURL, _photoData, _faceFusionAlpha, _callback) {
  97. start(_templateURL, _photoData, _faceFusionAlpha, _callback);
  98. }
  99. };
  100. };
  101. var faceFusionAPI = new faceFusionAPI();